package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.LogisticsCompany;
import com.mls.fivedistrict.server.LogisticsCompanyService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Service;
import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/5/17 16:28
 */
@RestController("backendLogisticsCompanyController")
@RequestMapping("/api/manager")
public class LogisticsCompanyController extends BaseController {
    @Autowired
    LogisticsCompanyService logisticsCompanyService;

    /**
     * 新增物流公司
     * @param logisticsCompany
     * @return
     */
    @PostMapping("/v1/logisticsCompany")
    public ServiceResponse addLogisticsCompany(@RequestBody LogisticsCompany logisticsCompany){
        BeanValidator.check(logisticsCompany);
        boolean flag = logisticsCompanyService.insert(logisticsCompany);
        if(flag){
            return  ServiceResponse.createSuccessResponse("添加物流公司成功");
        }else{
            return  ServiceResponse.createErrorResponse("添加物流公司失败");
        }
    }

    /**
     * 根据id修改物流公司
     * @param logisticsCompanyId
     * @param logisticsCompany
     * @return
     */
    @PutMapping("/v1/logisticsCompany/{logisticsCompanyId}")
    public ServiceResponse editLogisticsCompany(@PathVariable Long logisticsCompanyId,@RequestBody LogisticsCompany logisticsCompany){
        Optional.ofNullable(logisticsCompanyId).orElseThrow(() ->new CustomerException("要修改的物流公司id不能为空"));
        Optional.ofNullable(logisticsCompany).orElseThrow(() ->new CustomerException("要修改的物流公司不能为空"));
        logisticsCompany.setId(logisticsCompanyId);
        boolean flag = logisticsCompanyService.updateById(logisticsCompany);
        if(flag){
            return  ServiceResponse.createSuccessResponse("修改物流公司成功");
        }else{
            return ServiceResponse.createErrorResponse("修改物流公司失败");
        }
    }

    /**
     * 根据id删除物流公司
     * @param logisticsCompanyId
     * @return
     */
    @DeleteMapping("/v1/logisticsCompany/{logisticsCompanyId}")
    public ServiceResponse deleteLogisticsCompany(@PathVariable Long logisticsCompanyId){
        Optional.ofNullable(logisticsCompanyId).orElseThrow(() ->new CustomerException("要删除的物流公司id不能为空"));
        LogisticsCompany logisticsCompany = new LogisticsCompany();
        logisticsCompany.setIsDelete(true);
        logisticsCompany.setId(logisticsCompanyId);
        boolean flag = logisticsCompanyService.updateById(logisticsCompany);
        if(flag){
            return ServiceResponse.createSuccessResponse("删除物流公司成功");
        }else{
            return ServiceResponse.createErrorResponse("删除物流公司失败");
        }
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/v1/logisticsCompanies")
    public ServiceResponse<PageInfo<LogisticsCompany>> getLogisticsCompanyList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = logisticsCompanyService.selectList(pageFilter.getWrapper());
        PageInfo<LogisticsCompany> pageInfo = new PageInfo<LogisticsCompany>(selectList,5);
        return  ServiceResponse.createSuccessResponse(pageInfo);
    }
    @GetMapping("/v1/selectLogisticsCompanies")
    public ServiceResponse<List<LogisticsCompany>> getSelectLogistics(){
        List<LogisticsCompany> logisticsCompanyList = logisticsCompanyService.selectDropDownList();
        return ServiceResponse.createSuccessResponse(logisticsCompanyList);
    }
    /**
     * 根据id查询
     * @param logisticsCompanyId
     * @return
     */
    @GetMapping("/v1/logisticsCompany/{logisticsCompanyId}")
    public ServiceResponse getLogisticsCompany(@PathVariable Long logisticsCompanyId){
        Optional.ofNullable(logisticsCompanyId).orElseThrow(() ->new CustomerException("要查询的物流公司id不能为空"));
        LogisticsCompany logisticsCompany = logisticsCompanyService.selectById(logisticsCompanyId);
        if(null!=logisticsCompany){
            return ServiceResponse.createSuccessResponse(logisticsCompany);
        }else{
            return  ServiceResponse.createErrorResponse("不存在你要查询的物流公司");
        }
    }
}
